package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.Department;
import service.DepartmentService;
import java.util.List;
// 新增 Swagger 注解，描述 Controller 作用
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/department")
@Api(tags = "科室模块", description = "提供科室增删改查接口")  // 描述 Controller 功能
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/findAll")
    @ResponseBody
    @ApiOperation(value = "查询所有科室", notes = "获取系统中全部科室数据")  // 描述接口
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    @RequestMapping("/findById/{id}")
    @ResponseBody
    @ApiOperation(value = "根据 ID 查询科室", notes = "传入科室 ID，返回对应科室详情")
    public Department findById(@PathVariable int id) {
        return departmentService.findById(id);
    }

    @PostMapping("/add")  // 建议用 @PostMapping 更语义化，原 @RequestMapping 可保留但需指定 method
    @ResponseBody
    @ApiOperation(value = "新增科室", notes = "传入科室 JSON 数据，创建新科室")
    public int addDepartment(@RequestBody Department department) {
        return departmentService.addDepartment(department);
    }

    @PostMapping("/update")  // 同理，语义化请求方式
    @ResponseBody
    @ApiOperation(value = "修改科室", notes = "传入更新后的科室 JSON 数据，更新数据库")
    public int updateDepartment(@RequestBody Department department) {
        return departmentService.updateDepartment(department);
    }

    @DeleteMapping("/delete/{id}")  // 语义化删除
    @ResponseBody
    @ApiOperation(value = "删除科室", notes = "传入科室 ID，逻辑删除对应科室")
    public int deleteDepartment(@PathVariable int id) {
        return departmentService.deleteDepartment(id);
    }
}